import { createRouter, createWebHashHistory } from 'vue-router'
import { Toast } from 'vant'
import store from '../store/index.js'

// 采取路由懒加载模式
const Home = ()=> import(/* webpackChunkName: "home" */ '@/views/home/Home.vue')
const Catrgory = ()=> import(/* webpackChunkName: "home" */ '@/views/category/Category.vue')
const Detail = ()=> import(/* webpackChunkName: "home" */ '@/views/detail/Detail.vue')
const Profile = ()=> import(/* webpackChunkName: "home" */ '@/views/profile/Profile.vue')
const ShopCart = () => import(/* webpackChunkName: "home" */ '@/views/shopcart/ShopCart.vue')
const Register = () => import(/* webpackChunkName: "home" */ '@/views/profile/Register.vue')
const Login = () => import(/* webpackChunkName: "home" */ '@/views/profile/Login.vue')
const Address =  () => import(/* webpackChunkName: "address" */ '@/views/profile/Address.vue')
const AddressEdit =  () => import(/* webpackChunkName: "address" */ '@/views/profile/AddressEdit.vue')
const CreateOrder =  () => import(/* webpackChunkName: "order" */ '@/views/order/CreateOrder.vue')
const OrderDetail =  () => import(/* webpackChunkName: "order" */ '@/views/order/OrderDetail.vue')
const Order = () => import(/* webpackChunkName: "order" */ '@/views/order/order.vue')
const collect = () => import(/* webpackChunkName: "about" */ '@/views/profile/Collect.vue')
const Setting = () => import(/* webpackChunkName: "about" */ '@/views/profile/Setting.vue')
const About = () => import(/* webpackChunkName: "about" */ '@/views/profile/About.vue')

// 声明路由规则
const routes = [
  { path: '', name: 'DefaultHome', component: Home, meta: {title: '天天图书'} },
  { path: '/home', name: 'Home', component: Home , meta: {title: '天天图书'}},
  { path: '/category', name: 'Catrgory', component: Catrgory , meta: {title: '天天图书-图书分类'}},
  { path: '/detail', name: 'Detail', component: Detail , meta: {title: '天天图书-图书详情'}},
  { path: '/profile', name: 'Profile', component: Profile , meta: {title: '天天图书-个人中心', isAuthRequired: true}},
  { path: '/register', name: 'Register', component: Register , meta: {title: '天天图书-用户注册'}},
  { path: '/login', name: 'Login', component: Login , meta: {title: '天天图书-用户登录'}},
  { path: '/shopcart', name: 'shopCart', component: ShopCart , meta: {title: '天天图书-购物车', isAuthRequired: true}},
  { path: '/address', name: 'Address', component: Address , meta: {title: '天天图书-地址管理', isAuthRequired: true}},
  { path: '/addressedit', name: 'AddressEdit', component: AddressEdit , meta: {title: '天天图书-编辑地址', isAuthRequired: true}},
  { path: '/createorder', name: 'CreateOrder', component: CreateOrder , meta: {title: '天天图书-订单预览', isAuthRequired: true}},
  { path: '/orderdetail', name: 'OrderDetail', component: OrderDetail , meta: {title: '天天图书-订单详情', isAuthRequired: true}},
  { path: '/order', name: 'Order', component: Order , meta: {title: '天天图书-订单列表', isAuthRequired: true}},
  { path: '/collect', name: 'collect', component: collect , meta: {title: '天天图书-我的收藏', isAuthRequired: true}},
  { path: '/setting', name: 'Setting', component: Setting , meta: {title: '天天图书-账号管理', isAuthRequired: true}},
  { path: '/about', name: 'About', component: About , meta: {title: '天天图书-关于我们', isAuthRequired: true}},
]

// 创建路由对象
const router = createRouter({
  // 这里路由使用的是 hash模式
  history: createWebHashHistory(process.env.BASE_URL),
  routes
})

// 声明导航守卫
router.beforeEach((to, from, next) => {
  // 如果没有登录，在跳转到 login
  if (to.meta.isAuthRequired && store.state.user.isLogin === false) {
    Toast.fail('请先登录')
    setTimeout(() => {
      return next('/login')
    }, 500)
  } else {
    next()
  }
  
  document.title = to.meta.title
})


// 向外共享路由对象
export default router
